package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.SpacesItemDecoration;
import com.hengxinguotong.zhihuichengjian.adapter.WorkTypeAdapter;
import com.hengxinguotong.zhihuichengjian.bean.WorkType;
import com.hengxinguotong.zhihuichengjian.bean.WorkTypeRes;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author wulei
 * @Date 2018/9/12
 */

public class WorktypeListActivity extends BaseActivity implements View.OnClickListener, WorkTypeAdapter.OnItemClickListener
{
    private static final String TAG = "WorktypeListActivity";

    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.worktype_rv)
    RecyclerView worktypeRv;

    private Intent intent;
    private List<WorkType> workTypeList;
    private WorkTypeAdapter adapter;
    private WorkType workType = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_worktype_list);
        ButterKnife.bind(this);
        initView();
        getWorkType("加载中…");
    }

    private void initView(){
        intent = getIntent();
        String title = intent.getStringExtra("title");
        titleTv.setText(title);
        workTypeList = new ArrayList<>();
        adapter = new WorkTypeAdapter(this, workTypeList);
        worktypeRv.setAdapter(adapter);
        worktypeRv.setItemAnimator(new DefaultItemAnimator());
        worktypeRv.addItemDecoration(new SpacesItemDecoration(10));
        worktypeRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter.setOnItemClickListener(this);
    }

    /*
     *查询工种岗位
     */
    private void getWorkType(String alert) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("constructionId", SPUtil.getString(this, "constructionid"));
        Utils.requestData(alert, this, "/person/queryWorkType/" + SPUtil.getString(this,"uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    WorkTypeRes workTypeRes = gson.fromJson(json, WorkTypeRes.class);
                    workTypeList = workTypeRes.getValue();
                    adapter.setDataList(workTypeList);
                }
            }

            @Override
            public void onFailure(String json) {

            }
        });
    }

    @OnClick({R.id.iv_back, R.id.add})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.add:
                if(workType == null){
                    showToast("请选择岗位工种");
                    return;
                }
                intent.putExtra("workType", workType);
                setResult(0, intent);
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(View view, Object o, int position) {
        workType = workTypeList.get(position);
        for(WorkType temp:workTypeList){
            if(temp.getId().equals(workType.getId())){
                temp.setSelected(true);
            }else{
                temp.setSelected(false);
            }
        }
        adapter.setDataList(workTypeList);
    }
}
